{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn import svm\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Data Set Shape  (14999, 10)\n",
      "['satisfaction_level', 'last_evaluation', 'number_project', 'average_montly_hours', 'time_spend_company', 'Work_accident', 'left', 'promotion_last_5years', 'sales', 'salary']\n",
      " Sample Data     satisfaction_level  last_evaluation  number_project  average_montly_hours  \\\n",
      "0                0.38             0.53               2                   157   \n",
      "1                0.80             0.86               5                   262   \n",
      "2                0.11             0.88               7                   272   \n",
      "3                0.72             0.87               5                   223   \n",
      "4                0.37             0.52               2                   159   \n",
      "\n",
      "   time_spend_company  Work_accident  left  promotion_last_5years  sales  \\\n",
      "0                   3              0     1                      0  sales   \n",
      "1                   6              0     1                      0  sales   \n",
      "2                   4              0     1                      0  sales   \n",
      "3                   5              0     1                      0  sales   \n",
      "4                   3              0     1                      0  sales   \n",
      "\n",
      "   salary  \n",
      "0     low  \n",
      "1  medium  \n",
      "2  medium  \n",
      "3     low  \n",
      "4     low  \n"
     ]
    }
   ],
   "source": [
    "hr_data = pd.read_csv('data/hr.csv', header=0)\n",
    "hr_data.head()\n",
    "hr_data = hr_data.dropna()\n",
    "print(\" Data Set Shape \", hr_data.shape)\n",
    "print(list(hr_data.columns))\n",
    "print(\" Sample Data \", hr_data.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       satisfaction_level  last_evaluation  number_project  \\\n",
      "12602                0.10             0.84               7   \n",
      "4889                 0.57             0.68               4   \n",
      "1572                 0.39             0.48               2   \n",
      "13375                0.91             0.68               4   \n",
      "879                  0.82             0.97               5   \n",
      "1578                 0.10             0.96               6   \n",
      "8765                 0.92             0.51               4   \n",
      "1509                 0.44             0.52               2   \n",
      "1896                 0.39             0.49               2   \n",
      "4520                 0.70             0.44               2   \n",
      "4112                 0.88             0.98               3   \n",
      "11675                0.17             0.61               6   \n",
      "1845                 0.43             0.56               2   \n",
      "4914                 0.74             0.53               5   \n",
      "14738                0.09             0.90               7   \n",
      "9281                 0.32             0.64               3   \n",
      "9500                 0.91             0.94               4   \n",
      "6311                 0.80             0.88               5   \n",
      "7496                 0.57             0.39               2   \n",
      "13895                0.69             0.85               3   \n",
      "13132                0.55             0.98               4   \n",
      "9526                 0.45             0.87               2   \n",
      "14114                0.96             0.62               4   \n",
      "5403                 0.68             0.81               3   \n",
      "5821                 0.72             0.84               3   \n",
      "3614                 0.91             0.68               3   \n",
      "2108                 0.74             0.76               5   \n",
      "13496                0.64             0.67               3   \n",
      "9440                 0.57             0.49               2   \n",
      "14946                0.10             0.84               5   \n",
      "...                   ...              ...             ...   \n",
      "3385                 0.79             0.55               3   \n",
      "4555                 1.00             0.59               3   \n",
      "1184                 0.46             0.45               2   \n",
      "6420                 0.68             0.54               3   \n",
      "5051                 0.62             0.52               3   \n",
      "5311                 0.86             0.75               5   \n",
      "2433                 0.87             0.84               5   \n",
      "6949                 0.87             0.57               3   \n",
      "13803                0.95             0.84               3   \n",
      "10583                0.91             0.88               3   \n",
      "769                  0.42             0.46               2   \n",
      "1685                 0.76             0.98               5   \n",
      "8322                 0.55             0.75               4   \n",
      "11111                0.94             0.73               4   \n",
      "11363                0.57             0.60               2   \n",
      "11636                0.41             0.67               6   \n",
      "14423                0.88             1.00               5   \n",
      "5578                 0.47             0.55               2   \n",
      "4426                 0.88             0.66               4   \n",
      "13526                0.66             0.77               4   \n",
      "466                  0.37             0.48               2   \n",
      "6265                 0.99             0.55               4   \n",
      "5734                 0.53             0.54               4   \n",
      "11284                0.90             0.51               4   \n",
      "11964                0.82             0.80               5   \n",
      "5191                 0.52             0.96               4   \n",
      "13418                0.49             0.65               4   \n",
      "5390                 0.66             0.73               5   \n",
      "860                  0.79             1.00               4   \n",
      "7270                 0.98             0.86               2   \n",
      "\n",
      "       average_montly_hours  time_spend_company  Work_accident  \\\n",
      "12602                   250                   4              0   \n",
      "4889                    154                   3              1   \n",
      "1572                    154                   3              0   \n",
      "13375                   132                   4              0   \n",
      "879                     263                   5              0   \n",
      "1578                    310                   5              0   \n",
      "8765                    167                   3              1   \n",
      "1509                    128                   3              0   \n",
      "1896                    127                   3              0   \n",
      "4520                    131                   2              0   \n",
      "4112                    160                   2              0   \n",
      "11675                   154                  10              1   \n",
      "1845                    129                   3              0   \n",
      "4914                    141                   2              0   \n",
      "14738                   289                   4              0   \n",
      "9281                    151                   3              0   \n",
      "9500                    264                   4              0   \n",
      "6311                    199                   4              0   \n",
      "7496                    145                   3              0   \n",
      "13895                   153                  10              0   \n",
      "13132                   185                   2              1   \n",
      "9526                    268                   4              1   \n",
      "14114                   253                   7              0   \n",
      "5403                    166                   2              0   \n",
      "5821                    167                   3              0   \n",
      "3614                    218                   3              1   \n",
      "2108                    104                   4              0   \n",
      "13496                   143                   6              0   \n",
      "9440                    237                   2              0   \n",
      "14946                   303                   5              0   \n",
      "...                     ...                 ...            ...   \n",
      "3385                    147                   3              0   \n",
      "4555                    168                   3              0   \n",
      "1184                    143                   3              0   \n",
      "6420                    270                   4              1   \n",
      "5051                    148                   3              0   \n",
      "5311                    157                   4              0   \n",
      "2433                    137                   2              0   \n",
      "6949                    149                   2              0   \n",
      "13803                   240                   7              0   \n",
      "10583                   189                   2              0   \n",
      "769                     141                   3              0   \n",
      "1685                    242                   5              0   \n",
      "8322                    166                   2              0   \n",
      "11111                   204                   2              0   \n",
      "11363                   163                   3              0   \n",
      "11636                   221                  10              0   \n",
      "14423                   219                   5              0   \n",
      "5578                    156                   2              0   \n",
      "4426                    133                   2              0   \n",
      "13526                   188                   2              0   \n",
      "466                     134                   3              0   \n",
      "6265                    179                   4              0   \n",
      "5734                    257                   3              1   \n",
      "11284                   137                   6              0   \n",
      "11964                   205                   3              0   \n",
      "5191                    246                   3              0   \n",
      "13418                   233                   7              0   \n",
      "5390                    249                   2              0   \n",
      "860                     218                   5              0   \n",
      "7270                    219                   4              0   \n",
      "\n",
      "       promotion_last_5years  salary_high  salary_low  salary_medium  \\\n",
      "12602                      0            0           1              0   \n",
      "4889                       0            0           0              1   \n",
      "1572                       0            0           1              0   \n",
      "13375                      0            0           0              1   \n",
      "879                        0            0           0              1   \n",
      "1578                       0            0           1              0   \n",
      "8765                       0            0           0              1   \n",
      "1509                       0            0           0              1   \n",
      "1896                       0            0           0              1   \n",
      "4520                       0            0           1              0   \n",
      "4112                       0            0           0              1   \n",
      "11675                      0            0           0              1   \n",
      "1845                       0            0           1              0   \n",
      "4914                       0            0           1              0   \n",
      "14738                      0            0           1              0   \n",
      "9281                       0            0           1              0   \n",
      "9500                       0            0           1              0   \n",
      "6311                       0            0           1              0   \n",
      "7496                       0            0           0              1   \n",
      "13895                      0            0           0              1   \n",
      "13132                      0            0           1              0   \n",
      "9526                       0            0           1              0   \n",
      "14114                      0            0           0              1   \n",
      "5403                       0            0           0              1   \n",
      "5821                       0            0           0              1   \n",
      "3614                       0            0           0              1   \n",
      "2108                       0            0           1              0   \n",
      "13496                      0            0           0              1   \n",
      "9440                       0            0           1              0   \n",
      "14946                      0            0           0              1   \n",
      "...                      ...          ...         ...            ...   \n",
      "3385                       0            0           0              1   \n",
      "4555                       1            0           1              0   \n",
      "1184                       0            0           1              0   \n",
      "6420                       0            0           1              0   \n",
      "5051                       0            0           0              1   \n",
      "5311                       0            0           1              0   \n",
      "2433                       0            0           1              0   \n",
      "6949                       0            0           0              1   \n",
      "13803                      0            0           0              1   \n",
      "10583                      0            0           0              1   \n",
      "769                        0            0           0              1   \n",
      "1685                       0            0           1              0   \n",
      "8322                       0            0           1              0   \n",
      "11111                      0            0           1              0   \n",
      "11363                      0            0           0              1   \n",
      "11636                      0            0           0              1   \n",
      "14423                      0            0           1              0   \n",
      "5578                       0            0           0              1   \n",
      "4426                       0            0           0              1   \n",
      "13526                      0            0           1              0   \n",
      "466                        0            0           0              1   \n",
      "6265                       0            0           1              0   \n",
      "5734                       0            0           1              0   \n",
      "11284                      0            0           0              1   \n",
      "11964                      0            0           1              0   \n",
      "5191                       0            0           1              0   \n",
      "13418                      0            0           0              1   \n",
      "5390                       0            0           0              1   \n",
      "860                        0            0           1              0   \n",
      "7270                       0            0           1              0   \n",
      "\n",
      "       sales_IT  sales_RandD  sales_accounting  sales_hr  sales_management  \\\n",
      "12602         0            0                 0         0                 0   \n",
      "4889          0            0                 0         0                 1   \n",
      "1572          0            0                 0         0                 0   \n",
      "13375         1            0                 0         0                 0   \n",
      "879           0            0                 0         0                 0   \n",
      "1578          0            0                 0         0                 0   \n",
      "8765          1            0                 0         0                 0   \n",
      "1509          0            0                 0         0                 0   \n",
      "1896          1            0                 0         0                 0   \n",
      "4520          0            0                 0         0                 0   \n",
      "4112          0            0                 0         0                 0   \n",
      "11675         0            0                 0         0                 0   \n",
      "1845          0            0                 0         0                 0   \n",
      "4914          0            0                 1         0                 0   \n",
      "14738         0            0                 0         0                 0   \n",
      "9281          0            0                 0         0                 0   \n",
      "9500          0            0                 0         0                 0   \n",
      "6311          0            0                 0         0                 0   \n",
      "7496          0            0                 0         0                 0   \n",
      "13895         0            0                 0         0                 0   \n",
      "13132         0            0                 0         0                 0   \n",
      "9526          1            0                 0         0                 0   \n",
      "14114         0            0                 0         0                 0   \n",
      "5403          1            0                 0         0                 0   \n",
      "5821          0            0                 0         0                 0   \n",
      "3614          0            0                 1         0                 0   \n",
      "2108          0            0                 0         0                 0   \n",
      "13496         0            0                 0         0                 0   \n",
      "9440          0            0                 0         0                 0   \n",
      "14946         0            0                 1         0                 0   \n",
      "...         ...          ...               ...       ...               ...   \n",
      "3385          0            0                 0         0                 0   \n",
      "4555          0            0                 0         0                 0   \n",
      "1184          0            0                 0         0                 0   \n",
      "6420          0            0                 0         0                 0   \n",
      "5051          0            0                 0         0                 0   \n",
      "5311          0            0                 0         0                 0   \n",
      "2433          0            0                 0         0                 0   \n",
      "6949          0            0                 1         0                 0   \n",
      "13803         0            0                 0         0                 0   \n",
      "10583         0            0                 0         0                 0   \n",
      "769           0            0                 0         0                 0   \n",
      "1685          0            0                 0         0                 1   \n",
      "8322          0            0                 0         0                 0   \n",
      "11111         0            0                 0         0                 0   \n",
      "11363         0            0                 0         0                 0   \n",
      "11636         0            0                 0         0                 0   \n",
      "14423         0            0                 0         0                 0   \n",
      "5578          0            0                 0         0                 1   \n",
      "4426          0            1                 0         0                 0   \n",
      "13526         0            0                 0         0                 0   \n",
      "466           0            0                 0         0                 0   \n",
      "6265          0            0                 0         0                 0   \n",
      "5734          0            0                 0         0                 0   \n",
      "11284         0            0                 0         0                 0   \n",
      "11964         1            0                 0         0                 0   \n",
      "5191          0            0                 0         0                 0   \n",
      "13418         0            0                 0         0                 0   \n",
      "5390          0            0                 0         0                 0   \n",
      "860           0            0                 0         0                 0   \n",
      "7270          0            0                 0         0                 0   \n",
      "\n",
      "       sales_marketing  sales_product_mng  sales_sales  sales_support  \\\n",
      "12602                0                  1            0              0   \n",
      "4889                 0                  0            0              0   \n",
      "1572                 0                  0            0              0   \n",
      "13375                0                  0            0              0   \n",
      "879                  0                  0            0              0   \n",
      "1578                 0                  0            0              1   \n",
      "8765                 0                  0            0              0   \n",
      "1509                 0                  0            0              0   \n",
      "1896                 0                  0            0              0   \n",
      "4520                 0                  0            1              0   \n",
      "4112                 0                  1            0              0   \n",
      "11675                0                  0            1              0   \n",
      "1845                 0                  0            1              0   \n",
      "4914                 0                  0            0              0   \n",
      "14738                0                  1            0              0   \n",
      "9281                 0                  0            0              1   \n",
      "9500                 0                  0            0              0   \n",
      "6311                 0                  0            0              1   \n",
      "7496                 0                  0            1              0   \n",
      "13895                0                  0            0              1   \n",
      "13132                1                  0            0              0   \n",
      "9526                 0                  0            0              0   \n",
      "14114                0                  0            1              0   \n",
      "5403                 0                  0            0              0   \n",
      "5821                 0                  0            1              0   \n",
      "3614                 0                  0            0              0   \n",
      "2108                 0                  0            0              1   \n",
      "13496                0                  0            1              0   \n",
      "9440                 0                  0            0              1   \n",
      "14946                0                  0            0              0   \n",
      "...                ...                ...          ...            ...   \n",
      "3385                 0                  0            1              0   \n",
      "4555                 0                  0            0              1   \n",
      "1184                 0                  0            0              0   \n",
      "6420                 0                  0            1              0   \n",
      "5051                 0                  0            1              0   \n",
      "5311                 0                  0            0              1   \n",
      "2433                 0                  1            0              0   \n",
      "6949                 0                  0            0              0   \n",
      "13803                0                  0            1              0   \n",
      "10583                0                  0            0              1   \n",
      "769                  1                  0            0              0   \n",
      "1685                 0                  0            0              0   \n",
      "8322                 0                  0            1              0   \n",
      "11111                0                  0            1              0   \n",
      "11363                0                  0            1              0   \n",
      "11636                1                  0            0              0   \n",
      "14423                0                  0            0              0   \n",
      "5578                 0                  0            0              0   \n",
      "4426                 0                  0            0              0   \n",
      "13526                0                  0            0              0   \n",
      "466                  1                  0            0              0   \n",
      "6265                 1                  0            0              0   \n",
      "5734                 1                  0            0              0   \n",
      "11284                0                  0            1              0   \n",
      "11964                0                  0            0              0   \n",
      "5191                 0                  0            0              1   \n",
      "13418                0                  0            1              0   \n",
      "5390                 0                  0            0              1   \n",
      "860                  0                  0            1              0   \n",
      "7270                 0                  0            1              0   \n",
      "\n",
      "       sales_technical  \n",
      "12602                0  \n",
      "4889                 0  \n",
      "1572                 1  \n",
      "13375                0  \n",
      "879                  1  \n",
      "1578                 0  \n",
      "8765                 0  \n",
      "1509                 1  \n",
      "1896                 0  \n",
      "4520                 0  \n",
      "4112                 0  \n",
      "11675                0  \n",
      "1845                 0  \n",
      "4914                 0  \n",
      "14738                0  \n",
      "9281                 0  \n",
      "9500                 1  \n",
      "6311                 0  \n",
      "7496                 0  \n",
      "13895                0  \n",
      "13132                0  \n",
      "9526                 0  \n",
      "14114                0  \n",
      "5403                 0  \n",
      "5821                 0  \n",
      "3614                 0  \n",
      "2108                 0  \n",
      "13496                0  \n",
      "9440                 0  \n",
      "14946                0  \n",
      "...                ...  \n",
      "3385                 0  \n",
      "4555                 0  \n",
      "1184                 1  \n",
      "6420                 0  \n",
      "5051                 0  \n",
      "5311                 0  \n",
      "2433                 0  \n",
      "6949                 0  \n",
      "13803                0  \n",
      "10583                0  \n",
      "769                  0  \n",
      "1685                 0  \n",
      "8322                 0  \n",
      "11111                0  \n",
      "11363                0  \n",
      "11636                0  \n",
      "14423                1  \n",
      "5578                 0  \n",
      "4426                 0  \n",
      "13526                1  \n",
      "466                  0  \n",
      "6265                 0  \n",
      "5734                 0  \n",
      "11284                0  \n",
      "11964                0  \n",
      "5191                 0  \n",
      "13418                0  \n",
      "5390                 0  \n",
      "860                  0  \n",
      "7270                 0  \n",
      "\n",
      "[10499 rows x 20 columns]\n"
     ]
    }
   ],
   "source": [
    "data_trnsf = pd.get_dummies(hr_data, columns =['salary', 'sales'])\n",
    "data_trnsf.columns\n",
    "X = data_trnsf.drop('left', axis=1)\n",
    "X.columns\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, Y_train, Y_test = train_test_split(X, data_trnsf.left, test_size=0.3, random_state=42)\n",
    "print(X_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=1, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape='ovr', degree=3, gamma=1, kernel='linear',\n",
       "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "  tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "attrition_svm = svm.SVC(kernel='linear', C=1, gamma=1) \n",
    "attrition_svm.fit(X_train, Y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3212  216]\n",
      " [ 811  261]]\n"
     ]
    }
   ],
   "source": [
    "Y_pred = attrition_svm.predict(X_test)\n",
    "\n",
    "from sklearn.metrics import confusion_matrix\n",
    "confusionmatrix = confusion_matrix(Y_test, Y_pred)\n",
    "print(confusionmatrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy of SVM classifier on test set: 0.77\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.80      0.94      0.86      3428\n",
      "          1       0.55      0.24      0.34      1072\n",
      "\n",
      "avg / total       0.74      0.77      0.74      4500\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('Accuracy of SVM classifier on test set: {:.2f}'.format(attrition_svm.score(X_test, Y_test)))\n",
    "\n",
    "from sklearn.metrics import classification_report\n",
    "print(classification_report(Y_test, Y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
